package com.droidblackbox.history;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

import com.droidblackbox.R;
import com.droidblackbox.manager.FileManager;

public class HistoryRecordPlayer extends Activity implements SurfaceHolder.Callback {

	private String fileName;

	private MediaPlayer mediaPlayer = null;
	private SurfaceView surfaceView = null;
	private SurfaceHolder surfaceHolder = null;

	private LinearLayout linearLayoutSeekBar = null;
	private Button playAndPause = null;
	private SeekBar seekBar = null;
	private TextView textView = null;

	private int isVisible = LinearLayout.GONE;
	private int isChecked = 0;
	private boolean isStart = false;
	private boolean isPlaying = true;

	private File mediaFile = null;

	private Thread timerThread = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.surfaceview);

		Intent intent = getIntent();

		fileName = intent.getStringExtra(FileManager.SELECT_FILE);

		getWindow().setFormat(PixelFormat.UNKNOWN);

		initSurface();
		initTimer();

		addSubLayout(R.layout.player_control);

		linearLayoutSeekBar = (LinearLayout) findViewById(R.id.linearLayoutSeekBar);
		linearLayoutSeekBar.setVisibility(isVisible);

		seekBar = (SeekBar) findViewById(R.id.seekBar1);

		textView = (TextView) findViewById(R.id.textView1);
		textView.setText("");

		playAndPause = (Button) findViewById(R.id.playAndStop);
		playAndPause.setBackgroundColor(Color.argb(100, 0, 0, 0));
		playAndPause.setText("");
		playAndPause.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				Thread thread = new Thread() {
					private int loop = 0;
					private final int max = 1000;
					private final int time = 500;

					public void run() {

						while (loop < max) {
							try {
								isChecked = 1;
								Thread.sleep(time);
								loop += time;
							} catch (InterruptedException e) {
								// TODO: handle exception
							}
						}
						isVisible = LinearLayout.GONE;
						isChecked = 0;
					}
				};

				switch (isChecked) {
				case 0:

					mediaPlayer.start();
					textView.setText("Play");
					isVisible = LinearLayout.VISIBLE;

					thread.start();

					break;
				case 1:
					textView.setText("Pause");
					mediaPlayer.pause();
					isChecked = 0;
					isVisible = LinearLayout.GONE;
					break;

				}

				linearLayoutSeekBar.setVisibility(isVisible);
			}
		});

	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {

	}

	public void surfaceCreated(SurfaceHolder holder) {
		showRecording();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		isPlaying = false;
		mediaPlayer.stop();
		mediaPlayer.release();
		mediaPlayer = null;

		Intent intent = getIntent();
		setResult(RESULT_OK, intent);
		finish();
	}

	private void initSurface() {
		surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
		surfaceHolder = surfaceView.getHolder();
		surfaceHolder.addCallback(this);
		surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	private void addSubLayout(int layout) {
		LayoutInflater layoutInflater = LayoutInflater.from(getBaseContext());
		View view = layoutInflater.inflate(layout, null);
		LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);

		this.addContentView(view, layoutParams);

	}

	private void onSeekBar() {
		seekBar.setMax(mediaPlayer.getDuration());
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub

			}

			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				mediaPlayer.seekTo(seekBar.getProgress());
			}

			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if (fromUser)
					mediaPlayer.seekTo(progress);

			}
		});
	}

	private void showRecording() {
		if (isStart == false) {
			mediaPlayer = new MediaPlayer();

			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			mediaPlayer.setDisplay(surfaceHolder);

			try {
				File file = FileManager.getFile(FileManager.FOLDER_RECORD);
				mediaFile = new File(file, fileName);

				mediaPlayer.setDataSource(mediaFile.getAbsolutePath());

				mediaPlayer.prepare();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			mediaPlayer.start();
			timerThread.start();
			isStart = true;
			onSeekBar();
		}
	}

	private void initTimer() {
		timerThread = new Thread() {

			public void run() {
				try {
					while (isPlaying) {
						Thread.sleep(10);
						seekBar.setProgress(mediaPlayer.getCurrentPosition());
					}
				} catch (NullPointerException e) {
				} catch (IllegalStateException e) {

				} catch (InterruptedException e) {
					// TODO: handle exception
				}
			}
		};
	}

}
